<script src="../main.js"></script>
<template>
  <div>
    <el-container>
      <el-header>
        <img class="mlogo" src="../assets/hxy.png" />
      </el-header>

      <!--登录表单-->
      <el-main>
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="用户名：" prop="username">
            <el-input v-model="ruleForm.username"></el-input>
          </el-form-item>

          <el-form-item label="密码：" prop="password">
            <el-input type="password" v-model="ruleForm.password" @keypress.enter.native="submitForm(ruleForm)"></el-input>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="submitForm(ruleForm)"
              >立即登录</el-button
            >
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </el-main>
    </el-container>
  </div>
</template>

<script>
import request from "@/api/request.js";
export default {
  data() {
    return {
      message: "",
      ruleForm: {
        username: "admin",
        password: "zhonghao",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名！", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 15 个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码！", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    //登录
    submitForm(form) {
      request.post("/login", form).then((res) => {
        if (res.data.code == 200) {
          localStorage.setItem("token",res.data.data)
          localStorage.setItem("username",form.username)
          this.$router.push("main");
        } else {
          this.message=res.data.msg;
          this.$alert(this.message);
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style scoped>
.el-header,
.el-footer {
  /*background-color: #B3C0D1;*/
  color: #333;
  text-align: center;
  /*line-height: 600px;*/
  height: 100px !important;
}

.el-aside {
  background-color: #d3dce6;
  color: #333;
  text-align: center;
  line-height: 200px;
}

.el-main {
  /*background-color: #E9EEF3;*/
  color: #333;
  text-align: center;
  line-height: 160px;
}

body > .el-container {
  margin-bottom: 40px;
}

.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {
  line-height: 260px;
}

.el-container:nth-child(7) .el-aside {
  line-height: 320px;
}

.mlogo {
  height: 100px;
}

.demo-ruleForm {
  max-width: 400px;
  margin: 0 auto;
}
</style>
